package com.cbgolf.oa.views;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.recycle.StatisticsDetailsAdapter;
import com.cbgolf.oa.base.BaseViewNew;
import com.cbgolf.oa.contract.IStatisticsListDetailsContract.IStatisticsListDetailsView;
import com.cbgolf.oa.contract.IStatisticsListDetailsContract.IStatisticsListDetailsWaiter;
import com.cbgolf.oa.entity.ListBean;
import com.cbgolf.oa.event.EventStatistics;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DensityUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.RecyclerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lq
 * @date 2018/5/7
 */

public class StatisticsListDetailsViewImp extends BaseViewNew implements
    IStatisticsListDetailsView, OnRefreshLoadmoreListener {

  @BindView(R.id.topBack_ll)
  View topBack;
  @BindView(R.id.title_tv)
  TextView titleTv;

  @BindView(R.id.refresh)
  SmartRefreshLayout refreshLayout;
  @BindView(R.id.recycleView)
  RecyclerView recyclerView;

  /**
   * worker
   */
  private IStatisticsListDetailsWaiter worker;
  /**
   * 对应不同的数据
   */
  private int mType;

  private StatisticsDetailsAdapter adapter;

  private ErrorView errorView;

  private ListBean mData;


  public StatisticsListDetailsViewImp(Activity act, IStatisticsListDetailsWaiter waiter, int type,
      ListBean data) {
    this.worker = waiter;
    mType = type;
    this.mData = data;
    ButterKnife.bind(this, act);
    super.init(act);
  }

  @Override
  protected void setViews() {
    ViewUtils.setText(titleTv, worker.getActivityTitle());
    errorView = new ErrorView(context);
    refreshLayout.setEnableLoadmore(mType == DataManager.STATISTICS_CADDIE_ALL);
    refreshLayout.setEnableRefresh(mType == DataManager.STATISTICS_CADDIE_ALL);
    ViewUtils.setRefreshCircleHeader(context, refreshLayout);
    refreshLayout.setOnRefreshLoadmoreListener(this);
    setListAdapter();
  }


  @Override
  protected void setListener() {
    ViewUtils.setClick(topBack, v -> worker.back());
  }

  private void setListAdapter() {
    adapter = new StatisticsDetailsAdapter(context, mType);
    LinearLayoutManager lm = new LinearLayoutManager(context);
    lm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(lm);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);

  }

  @Override
  public void showProgress() {
    if (refreshLayout.isRefreshing() || refreshLayout.isLoading()) {
      return;
    }
    AnimaUtil.showLoading(context);
  }

  @Override
  public void stopProgress() {
    if (null != refreshLayout) {
      refreshLayout.finishLoadmore();
      refreshLayout.finishRefresh();
    }
    AnimaUtil.stopLoading();
  }

  @Override
  public void requestFail(String msg, int code) {
    if (null != errorView) {
      errorView.showError(refreshLayout, msg, code, null);
    }

  }

  @Override
  public void requestOver(EventStatistics event) {
    if (null == event) {
      return;
    }
    stopProgress();
    switch (event.msgTarget) {
      case DataManager.STATISTICS_BOOK_DETAILS:
        if (null == event.playerData) {
          requestFail(event.errorMsg, event.errorCode);
          return;
        }
        showDataVisible();
        showBookData(event);
        break;
      case DataManager.STATISTICS_CADDIE_DETAILS:
        showDataVisible();
        showCaddieData(event);
        break;
      case DataManager.STATISTICS_BILL_DETAILS:
        showDataVisible();
        showBillData(event);
        break;
      case DataManager.STATISTICS_BUSINESS_DETAILS:
      case DataManager.STATISTICS_PLAYER_DETAILS:
        if (null == event.playerData) {
          requestFail(event.errorMsg, event.errorCode);
          return;
        }
        showDataVisible();
        showData(event);
        break;
      default:
        break;
    }
  }

  private void showBillData(EventStatistics event) {
    List<ListBean> list = new ArrayList<>();
    ListBean data;
    for (int i = 0; i < DataManager.STATISTICS_DETAILS_SIZE; i++) {
      data = new ListBean();
      switch (i) {
        case 0:
          data = event.playerData;
          break;
        case 1:
          data.customerItemInfoDtoList = event.playerData.customerItemInfoDtoList;
          break;
        case 2:
          data.consumerItems = event.playerData.consumerItems;
          break;
        case 3:
          data.cashWays = event.playerData.cashWays;
          break;
        case 4:
          ListBean.CustomerInfoDto info = new ListBean().getCustomerInfoDto();
          info.remark = data.remark;
          data.customerInfoDto = info;
          break;
        default:
          break;
      }
      list.add(data);
    }
    adapter.setData(list, (null != mData) ? mData.orderClassification : "");

  }

  /**
   * show caddie statistics items
   */
  private void showCaddieData(EventStatistics event) {
    recyclerView.addItemDecoration(new RecyclerItemDecoration(DensityUtil.dip2px(context, 5)
        , 0, 0, 0));
//    List<ListBean> list = new ArrayList<>();
    switch (event.requestEvent) {
      case DataUtil.INITDATA:
        if (Util.listIsNull(event.listCaddie)) {
          requestFail(event.errorMsg, event.errorCode);
          return;
        }
//        list.addAll(event.listCaddie);
//        list.add(new ListBean());
        adapter.setData(event.listCaddie);
        break;
      case DataUtil.REFRESH:
        if (Util.listIsNull(event.listCaddie)) {
          return;
        }
        adapter.setData(event.listCaddie);
        break;
      case DataUtil.LOADMORE:
        adapter.addAll(event.listCaddie);
        break;
      default:
        break;
    }
  }

  /**
   * show book statistics items
   */
  private void showBookData(EventStatistics event) {
    List<ListBean> list = new ArrayList<>();
    ListBean data;
    for (int i = 0; i < DataManager.STATISTICS_BOOK_SIZE; i++) {
      data = new ListBean();
      switch (i) {
        case 0:
          data.reserveInfoDtoList = event.playerData.reserveInfoDtoList;
          break;
        case 1:
          data.customerItemInfoDtoList = event.playerData.customerItemInfoDtoList;
          break;
        case 2:
          data.payInfoDtoList = event.playerData.payInfoDtoList;
          break;
        case 3:
//          ListBean.CustomerInfoDto info = new ListBean().getCustomerInfoDto();
//          info.remark = data.remark;
          data.remark = event.playerData.remark;
          break;
        default:
          break;
      }
      list.add(data);
    }
    adapter.setData(list, (null != mData) ? mData.orderClassification : "");
  }

  private void showDataVisible() {
    if (null != errorView) {
      errorView.showData(refreshLayout);
    }
  }

  /**
   * show public items
   */
  private void showData(EventStatistics event) {

    List<ListBean> list = new ArrayList<>();
    ListBean data;
    for (int i = 0; i < DataManager.STATISTICS_DETAILS_SIZE; i++) {
      data = new ListBean();
      switch (i) {
        case 0:
          data.customerInfoDto = event.playerData.customerInfoDto;
//              (event.msgTarget == DataManager.STATISTICS_BILL_ALL?
//              event.playerData :
//          event.playerData.customerInfoDto);
          break;
        case 1:
          data.teeTimeDtoList = (event.msgTarget == DataManager.STATISTICS_BUSINESS_DETAILS ||
              event.msgTarget == DataManager.STATISTICS_BILL_DETAILS ? null :
              event.playerData.teeTimeDtoList);
          break;
        case 2:
          data.customerItemInfoDtoList = event.playerData.customerItemInfoDtoList;
          break;
        case 3:
          data.payInfoDtoList = event.playerData.payInfoDtoList;
          break;
        case 4:
          data.customerInfoDto = event.playerData.customerInfoDto;
          break;
        default:
          break;
      }
      list.add(data);

    }
    adapter.setData(list);
  }

  @Override
  public void onLoadmore(RefreshLayout refreshlayout) {
    worker.loadMore();
  }

  @Override
  public void onRefresh(RefreshLayout refreshlayout) {
    worker.refresh();
  }
}
